/**
 * @typedef {ImageShapeOptions&{speed?:number}} BasketShapeOptions
 */
class BasketShape extends ImageShape {
  /**
   * @description 移动速度
   * @type {number}
   */
  speed;

  /**
   * @description 构造函数
   * @param {BasketShapeOptions} options
   */
  constructor(options) {
    super(options);
    this.speed = options.speed ?? 1;
  }

  /**
   * @description 键盘按下事件
   * @param {KeyboardEvent} ev
   */
  keydown(ev) {
    const { code } = ev;
    if (code === "ArrowLeft") {
      if (this.x > 0) {
        this.x -= this.speed;
      }
    } else if (code === "ArrowRight") {
      if (this.x < this.canvas.width - this.width) {
        this.x += this.speed;
      }
    }
  }

  /**
   * @description 控制
   * @returns {{cancelControl:()=>void}}
   */
  control() {
    const listen = this.keydown.bind(this);
    document.body.addEventListener("keydown", listen);
    return {
      cancelControl: () => {
        document.body.removeEventListener("keydown", listen);
      },
    };
  }

  /**
   * @description 检测接住那些水果
   * @param  {FruitShape[]} fruits
   */
  catch(...fruits) {
    return fruits.filter((fruit) => isIntersecting(fruit, this));
  }
}
